<?php
namespace Admin\Controller;
use Think\Controller;
class AddController extends BaseController {
	
    public function index(){
       if($_GET['sssid']){
            $s=$_GET['sssid'];

            $this->assign('s',$s);
        }
                
        $admin=M();
        $sdk_add=$admin->table('ymn_sdk_add')->select();
        $this->assign('sdk_add',$sdk_add);
        $this->display();
            
        
        
    }
   
    //编辑版本
    public function edit_version(){
        /*var_dump($file);*/
        header("Content-type:text/html;charset=utf-8");
        $id=I('post.id');
        $upload=$this->upload();
        $url=$upload['savepath'];
        
        
        $info['name']=I('post.name');
        $info['introduction']=I('post.textarea');
        $info['version']=I('post.version_sdk');
        $info['add_time']=date('Y-m-d H:i:s');
        $info['sdk_url']=$upload['photo']['savepath'].$upload['photo']['savename'];
        
        $admin=M();
        $result=$admin->table('ymn_sdk_add')->where("id='".$id."'")->save($info);
        
        $this->success("保存成功","/Add/index?sssid=".$id."");
       
    }
    //增加新版本
    public function add_newversion(){
        header("Content-type:text/html;charset=utf-8");
        $id=I('post.id');
        $upload=$this->upload();
        $url=$upload['savepath'];
        $info['match_id']=$id;
        $info['name']=I('post.name');
        $info['state']='1';
        $info['state_zx']='0';
        $info['introduction']=I('post.textarea');
        $info['version']=I('post.version_sdk');
        $info['add_time']=date('Y-m-d H:i:s');
        $info['sdk_url']=$upload['photo']['savepath'].$upload['photo']['savename'];
        
        $admin=M();
        $result=$admin->table('ymn_sdk_add')->add($info);
        
        
        $this->success("保存成功","/Add/index?sssid=".$id."");
      
    }
    public function sdk_state(){
        $id=I('post.id');

        $admin=M();
        $add=$admin->table('ymn_sdk_add')->where("id='".$id."'")->find(); 
        $match_id=$add['match_id'];
        //点击下线
        if(!$add['state']){
            $info['state_zx']='0';
            $info['state']='1';
            $info['release_time']='';
            $result=$admin->table('ymn_sdk_add')->where("id='".$id."'")->save($info);
            $info_adm['version']='';
            $info_adm['version_id']='';
            $info_adm['release_state']='';
            $info_adm['release_time']='';
            $info_adm['update_intro']='';

            
            $result=$admin->table('ymn_sdk_admin')->where("id='".$match_id."'")->save($info_adm);

            if($result){
                echo "下线成功";
            }
        }else{//点击上线
            //查找当前match_id是否有其他版本在线

           $result=$admin->table('ymn_sdk_add')->where("match_id='".$match_id."'AND state=0")->getfield('id');
            
            if($result){
                //如果有 下线其他版本
                $info['state_zx']='0';
                $info['state']='1';
                $info['release_time']='';
                $result=$admin->table('ymn_sdk_add')->where("id='".$result."'")->save($info);
                //上线该版本
                $info['state_zx']='1';
                $info['state']='0';

                $result=$admin->table('ymn_sdk_add')->where("id='".$id."'")->save($info);
                //保存上线版本到ymn_sdk_admin
                $info_adm['version']=$add['version'];
                $info_adm['version_id']=$id;
                $info_adm['release_time']='';
                $result=$admin->table('ymn_sdk_add')->where("id='".$id."'")->find();
                $info_adm['update_intro']=$result['introduction'];

                
                $result=$admin->table('ymn_sdk_admin')->where("id='".$match_id."'")->save($info_adm);
                
                if($result){
                    echo "上线成功";
                    
                }else{
                    echo "上线失败";
                }
            }else{
                //如果没有 上线该版本
                $info['state_zx']='1';
                $info['state']='0';
                $result=$admin->table('ymn_sdk_add')->where("id='".$id."'")->save($info);
                //保存上线版本到ymn_sdk_admin
                $info_adm['version']=$add['version'];
                $info_adm['version_id']=$id;
                $result=$admin->table('ymn_sdk_add')->where("id='".$id."'")->find();
                $info_adm['update_intro']=$result['introduction'];
                $result=$admin->table('ymn_sdk_admin')->where("id='".$match_id."'")->save($info_adm);
               
                if($result){
                    echo "上线成功";
                   
                }else{
                    echo "上线失败";
                   
                }
            }
        }       
    }
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/ymn_admin/upload/'; // 设置附件上传根目录
        //$upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功        
           return $info;
        }
    }
}
   
